package com.browsesoft.resource;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.Element;

import com.browsesoft.EntityManager;
import com.browsesoft.ManagerGroup;
import com.browsesoft.Tools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLTreeModel;
import com.browsesoft.user.Role;
import com.browsesoft.user.User;

public class LicenseFunctionTreeModel implements HTMLTreeModel {
	/**
	 * 功能树所对应的用户
	 */
	private Resource root = null;

	/**
	 * 拥有的资源
	 */
	public Collection resources = new LinkedList();

	/**
	 * 不拥有的资源
	 */
	public Collection notResources = new HashSet();

	public Collection allResources = new LinkedList();

	private User user;

	public LicenseFunctionTreeModel() {
	}

	/**
	 * 初始化model
	 * 
	 * @param request
	 *            请求
	 * @throws Exception
	 */
	public void init(HttpServletRequest request) throws Exception {
		user = (User) request.getSession().getAttribute("loginUser");
		resources = this.getRes(user);
		notResources = ResourceTool.getAllAncenstor(resources);
		this.allResources.addAll(resources);
		this.allResources.addAll(notResources);
		// 装载根资源
		EntityManager entitymanager = (EntityManager) ManagerGroup
				.getManagerGroup().getManager("entity");
		Resource rt = entitymanager.getRoot();
		Iterator iter = rt.getChildren();
		while (iter.hasNext()) {
			Resource res = (Resource) iter.next();
			String type = res.getType();
			System.out.println("type=" + type);
			if (type.equals("functiontree")) {
				root = res;
				break;
			}
		}
		this.resources = getRes();
		System.out.println("root=" + root);
	}

	/**
	 * 得到资源
	 * 
	 * @return 资源
	 * @throws Exception
	 */
	public Collection getRes(User user) throws Exception {
		Collection rs = LicensePolicyTool.getRights(user);
		LinkedList link = ((BasicResource) EntityManager.getInstance()
				.getRoot()).getSpecialResByType("role");
		Iterator iter = link.iterator();
		while (iter.hasNext()) {
			Role role = (Role) iter.next();
			LinkedList list = role.getUsers();
			if (list.contains(user)) {
				rs.addAll(LicensePolicyTool.getRights(role));
			}
		}
		return rs;
	}

	/**
	 * 处理用户请求
	 * 
	 * @param request
	 *            客户端请求
	 * @param response
	 *            响应
	 * @throws Exception
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	}

	/**
	 * 取得根结点
	 * 
	 * @return 根结点
	 * @throws Exception
	 */
	public Object getRootNode() throws Exception {
		return root;
	}

	/**
	 * 根据结点返回子结点
	 * 
	 * @return 子结点
	 */
	public LinkedList getChildNodes(Object node) throws Exception {
		LinkedList children = Tools.iteratorToList(((Resource) node).getChildren());
		// 从子中过虑掉无权访问的节点
		children.retainAll(this.allResources);
		return children;
	}

	/**
	 * 根据结点返回结点的名称
	 * 
	 * @param node
	 *            结点
	 * @return 结点的名称
	 * @throws Exception
	 */
	public String getName(Object node) throws Exception {
		return (String) ((Resource) node).getAttributes().get("name");
	}

	/**
	 * 得到节点对应的图标
	 * 
	 * @param node
	 *            节点
	 * @return 图标
	 */
	public String getIcon(Object node) {
		String icon = (String) ((Resource) node).getAttributes().get("f_icon");
		if (icon == null) {
			icon = "fc_default";
		}
		return icon;
	}

	/**
	 * 根据结点返回结点的id
	 * 
	 * @param node
	 *            结点
	 * @return 结点的名称
	 * @throws Exception
	 */
	public String getID(Object node) throws Exception {
		return ((Resource) node).getID();
	}

	/**
	 * 根据id找到结点对象
	 * 
	 * @param ID
	 *            对象id
	 * @return 结点对象
	 * @throws Exception
	 */
	public Object getObject(String ID) throws Exception {
		EntityManager entitymanager = (EntityManager) ManagerGroup
				.getManagerGroup().getManager("entity");
		return entitymanager.getObject(ID);
	}

	/**
	 * 根据结点返回结点的链接
	 * 
	 * @param node
	 *            结点
	 * @return 结点的链接
	 * @throws Exception
	 */
	public String getLink(Object node) throws Exception {
		return (String) ((Resource) node).getAttributes().get("link");
	}

	/**
	 * 设置模型所对应的组件
	 * 
	 * @param component
	 *            对应的组件
	 */
	public void setComponent(HTMLBasicComponent component) {
	}

	/**
	 * 模型更新页面元素
	 * 
	 * @param c
	 *            组件
	 * @throws Exception
	 */
	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}

	/**
	 * 设置链接内容
	 * 
	 * @param nodeLink
	 *            链接
	 * @param modelNode
	 *            结点
	 * @throws Exception
	 */
	public void setContextNode(Element nodeLink, Object modelNode)
			throws Exception {
		// 得到链接的onclick属性
		String onclick = nodeLink.getAttribute("onclick");
		// 得到对应功能的id号
		String fid = ((BasicResource) modelNode).getID();
		// 替换onclick中的functionid
		onclick = onclick.replaceFirst("functionid=", "functionid=" + fid);
		// 设置onclick属性
		nodeLink.setAttribute("onclick", onclick);
	}

	/**
	 * 得到资源
	 * 
	 * @return 资源
	 * @throws Exception
	 */
	public LinkedList getRes() throws Exception {
		LinkedList rs = user.getResourceTreeByRight();
		LinkedList link = ((BasicResource) EntityManager.getInstance()
				.getRoot()).getSpecialResByType("role");
		Iterator iter = link.iterator();
		while (iter.hasNext()) {
			Role role = (Role) iter.next();
			LinkedList list = role.getUsers();
			if (list.contains(user)) {
				rs.addAll(role.getResourceTreeByRight());
			}
		}
		return rs;
	}

	/**
	 * 判断是否有子
	 */
	public boolean hasChilds(Object obj) {
		Resource res = (Resource) obj;
		return res.getChildren().hasNext();
	}
	/**
	 * 设置跟资源
	 */
	public void setRoot() throws Exception {
		EntityManager entitymanager = (EntityManager) ManagerGroup
				.getManagerGroup().getManager("entity");
		Resource rt = entitymanager.getRoot();
		Iterator iter = rt.getChildren();
		while (iter.hasNext()) {
			Resource res = (Resource) iter.next();
			String type = res.getType();
			System.out.println("type=" + type);
			if (type.equals("functiontree")) {
				root = res;
				break;
			}
		}
	}
}